package labox.innovation.db;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.db.core.HeapByteBuffer;
import labox.innovation.db.core.SqlData;
import labox.innovation.db.manager.DBConnetionStateManager;
import labox.innovation.db.proxy.ConnectionProxy;
import labox.innovation.db.proxy.PreparedStatementProxy;
import labox.innovation.db.util.DbUtil;
import labox.innovation.db.util.SqlDataIdFactory;
import labox.innovation.gameserver.model.FuzeTrusteeInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

/**
 * �Զ�ս����ݿ������
 * @author lian
 */
public class CombatSetDAO {
	private static final Logger log = LoggerFactory.getLogger(CombatSetDAO.class);
	/**
	 * ������߸��½�ɫ�Զ�ս������
	 */
	private static final String UPDATE_COMBATSET = "replace into character_combatset (charId,details) values (?,?)";
	/**
	 * ���ؽ�ɫ�Զ�ս������
	 */
	private static final String SELECT_COMBATSET = "select details from character_combatset where charId=?";

	/**
	 * ɾ���ɫս������
	 */
	public static final String PHYSICAL_DELETE_COMBATSET_BY_ID="delete from character_combatset where charId=?";
	/**
	 * ������߸��½�ɫ�Զ�ս������
	 * @param trus
	 */
	public static final void saveOrUpadte(FuzePcInstance pc) {
		FuzeTrusteeInstance trus = pc.getAI().getTrustee();
		if (trus == null)
			return;
		ConnectionProxy con = null;
		PreparedStatementProxy ptmt = null;
		Object[] params = null;
		SqlData sqldata = null;
		HeapByteBuffer buf = HeapByteBuffer.newInstance();
		buf.clear();
		/*** ʰȡ ***/
		buf.writeInt(trus.getSolution());// ҩˮ 0�ر� 1���� Ĭ��1
		buf.writeInt(trus.getGemstone());
		buf.writeInt(trus.getScroll());
		buf.writeInt(trus.getFaEquipment());
		buf.writeInt(trus.getBaoEquipment());
		buf.writeInt(trus.getLingEquipment());
		buf.writeInt(trus.getZhiEquipment());
		buf.writeInt(trus.getShenEquipment());
		/*** ���ܻ�buffer ***/
		buf.writeInt(trus.getIsUseSkill());
		buf.writeInt(trus.getSkillId_1());
		buf.writeInt(trus.getSkillId_2());
		buf.writeInt(trus.getSkillId_3());
		buf.writeInt(trus.getSkillId_4());
		buf.writeInt(trus.getIsUseState());
		buf.writeInt(trus.getStateId_1());
		buf.writeInt(trus.getStateId_2());
		buf.writeInt(trus.getStateId_3());
		buf.writeInt(trus.getStateId_4());
		/*** ��ɫ�������� ***/
		buf.writeInt(trus.getHpoff());
		buf.writeInt(trus.getHpCP());
		buf.writeInt(trus.getSpoff());
		buf.writeInt(trus.getSpCP());
		buf.writeInt(trus.getAutoBuyRed());
		buf.writeInt(trus.getRedItemId());// ��ҩģ��id
		buf.writeInt(trus.getAutoBuyBlue());
		buf.writeInt(trus.getBlueItemId());
		buf.writeInt(trus.getHpBeastOff());
		buf.writeInt(trus.getHpBeast());
		buf.writeInt(trus.getHealSkillId());
		buf.writeInt(trus.getDealSpawn());
		buf.writeInt(trus.getSpawnSkillId());
		buf.writeInt(trus.getHpPetOff());
		buf.writeInt(trus.getHpPet());
		buf.writeInt(trus.getPetHealSkillId());
		buf.writeInt(trus.getPetSpawn());
		buf.writeInt(trus.getPetSpawnSkillId());
		/*** �������� ***/
		buf.writeInt(trus.getPointPK());
		buf.writeInt(trus.getAutofix());
		buf.writeInt(trus.getPkActorR());
		buf.writeInt(trus.getAutoTeam());
		buf.writeInt(trus.getAutoReply());
		buf.writeString(trus.getAutoMsg());
		/*** �ǹһ�ʱ��Ч���� ***/
		buf.writeInt(trus.getOffUserProtect());
		buf.writeInt(trus.getOffBeastProtect());
		buf.writeInt(trus.getOffPetProtect());
		buf.writeInt(trus.getOffPickFilter());
		buf.writeInt(trus.getOffAutoFix());
		buf.flip();
		try {
			if (!DBConnetionStateManager.getDbState()) {
				params = new Object[] { pc.getObjectId(), buf };
				sqldata = new SqlData(SqlDataIdFactory.createSqlId("update_character_combatset", pc.getObjectId()), UPDATE_COMBATSET, params);
				DBConnetionStateManager.addDbTask(sqldata);
			}
			con = ConnectionProxy.newInstance();
			if (con != null)
				ptmt = con.prepareStatement(UPDATE_COMBATSET);
			// ����Ƿ������д����ݿ�
			if (DbUtil.isDbNormal(DBConnetionStateManager.getDbState(), con, ptmt)) {
				ptmt.setLong(1, pc.getObjectId());
				ptmt.setBinaryStream(2, buf, buf.limit());
				int rs = ptmt.executeUpdate();
				if (rs != PreparedStatementProxy.NETWORKDISCONNECTION) {
					HeapByteBuffer.recycle(buf);
				} else {
					params = new Object[] { pc.getObjectId(), buf };
					sqldata = new SqlData(SqlDataIdFactory.createSqlId("update_character_combatset", pc.getObjectId()), UPDATE_COMBATSET, params);
					DBConnetionStateManager.addDbTask(sqldata);
				}
			} else {
				params = new Object[] { pc.getObjectId(), buf };
				sqldata = new SqlData(SqlDataIdFactory.createSqlId("update_character_combatset", pc.getObjectId()), UPDATE_COMBATSET, params);
				DBConnetionStateManager.addDbTask(sqldata);
			}
		} catch (SQLException e) {
			log.error("save combatset error palyerid=" + pc.getObjectId(), Config.SERVER_ID, e);// �ǵý�0���ɽ�ɫID
		} finally {
			if (ptmt != null)
				ptmt.close();
			if (con != null)
				con.close();
		}

	}

	public static final void load(FuzePcInstance pc) {
		FuzeTrusteeInstance trus = null;
		HeapByteBuffer buf = HeapByteBuffer.newInstance();
		buf.clear();
		ConnectionProxy con = null;
		PreparedStatementProxy ptmt = null;
		ResultSet rs = null;
		try {
			if (!DBConnetionStateManager.getDbState())
				return;
			con = ConnectionProxy.newInstance();
			if (con != null)
				ptmt = con.prepareStatement(SELECT_COMBATSET);
			if (!DbUtil.isDbNormal(DBConnetionStateManager.getDbState(), con, ptmt))
				return;
			ptmt.setLong(1, pc.getObjectId());
			byte[] bytes;
			rs = ptmt.executeQuery();
			while (rs.next()) {
				trus = pc.getAI().getTrustee();
				bytes = rs.getBytes("details");
				if (bytes.length == 0)
					break;
				buf.write(bytes, 0, bytes.length);
				buf.flip();
				/*** ʰȡ ***/
				trus.setSolution(buf.readInt());
				trus.setGemstone(buf.readInt());
				trus.setScroll(buf.readInt());
				trus.setFaEquipment(buf.readInt());
				trus.setBaoEquipment(buf.readInt());
				trus.setLingEquipment(buf.readInt());
				trus.setZhiEquipment(buf.readInt());
				trus.setShenEquipment(buf.readInt());
				/*** ���ܻ�buffer ***/
				trus.setIsUseSkill(buf.readInt());
				trus.setSkillId_1(buf.readInt());
				trus.setSkillId_2(buf.readInt());
				trus.setSkillId_3(buf.readInt());
				trus.setSkillId_4(buf.readInt());
				trus.setIsUseState(buf.readInt());
				trus.setStateId_1(buf.readInt());
				trus.setStateId_2(buf.readInt());
				trus.setStateId_3(buf.readInt());
				trus.setStateId_4(buf.readInt());
				/*** ��ɫ�������� ***/
				trus.setHpoff(buf.readInt());
				trus.setHpCP(buf.readInt());
				trus.setSpoff(buf.readInt());
				trus.setSpCP(buf.readInt());
				trus.setAutoBuyRed(buf.readInt());
				trus.setRedItemId(buf.readInt());
				trus.setAutoBuyBlue(buf.readInt());
				trus.setBlueItemId(buf.readInt());
				trus.setHpBeastOff(buf.readInt());
				trus.setHpBeast(buf.readInt());
				trus.setHealSkillId(buf.readInt());
				trus.setDealSpawn(buf.readInt());
				trus.setSpawnSkillId(buf.readInt());
				trus.setHpPetOff(buf.readInt());
				trus.setHpPet(buf.readInt());
				trus.setPetHealSkillId(buf.readInt());
				trus.setPetSpawn(buf.readInt());
				trus.setPetSpawnSkillId(buf.readInt());
				/*** �������� ***/
				trus.setPointPK(buf.readInt());
				trus.setAutofix(buf.readInt());
				trus.setPkActorR(buf.readInt());
				trus.setAutoTeam(buf.readInt());
				trus.setAutoReply(buf.readInt());
				trus.setAutoMsg(buf.readString());
				/*** �ǹһ�ʱ��Ч���� ***/
				trus.setOffUserProtect(buf.readInt());
				trus.setOffBeastProtect(buf.readInt());
				trus.setOffPetProtect(buf.readInt());
				trus.setOffPickFilter(buf.readInt());
				trus.setOffAutoFix(buf.readInt());
			}

		} catch (SQLException e) {
			log.error("load character combatset error playerid=" + pc.getObjectId(), Config.SERVER_ID, e);
		} finally {
			HeapByteBuffer.recycle(buf);
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if (ptmt != null)
				ptmt.close();
			if (con != null)
				con.close();
		}
	}
}
